package npr;


import processing.core.*;

import javax.media.opengl.*;
import com.sun.opengl.util.*;
import java.util.LinkedList;
import java.util.ListIterator;
import javax.media.opengl.*;
import com.sun.opengl.util.*;

/** 
 * This finds ridges in smooth models using derivates of curvature.
 * Ridges can be precomputed, as they are not view dependent and thus are very fast.
 * Note that this algorithm works only for models loaded from .obj files,
 * not for Processing drawing commands.
 *
 * @see SmoothValleys
 */
public class SmoothRidges extends ObjectSpaceLines{

    /**
     * Specifies whether lines should be drawn faded
     */
    protected boolean drawFaded= true;


    /** 
     * Creates a SmoothRidges object.
     */
    public SmoothRidges(){	
    }

    /**
     * Specifies whether lines should be drawn faded.
     *
     * @param drawFaded true if lines are to be faded, false if not.
     * The default is true.
     */
    public void drawFaded(boolean drawFaded){
	
	this.drawFaded= drawFaded;
    }
        
    /**
     * Requests the ridges from a model and draws them with OpenGL
     * visbility test.
     * You don't have to call this method. It is called by the renderer.
     */
    public void postProcess(){
		
	renderer= (NPR)parent.g;
	gl= renderer.gl;

	gl.glEnable(GL.GL_LINE_SMOOTH);

	OBJModel model;
	for(int i=start; i<stop; i++){
	    float modelview[]= renderer.getModelview(i);
	    gl.glMatrixMode(GL.GL_MODELVIEW);
	    gl.glPushMatrix();
	    gl.glTranslatef(0, 0, width);
	    gl.glMultMatrixf(modelview, 0);

	    ListIterator<OBJModel> listIterator= renderer.getModels(i).listIterator();

	    while(listIterator.hasNext()){    
		model= listIterator.next();
		int ridges= model.getSmoothRidges(true, true, drawFaded, threshold, red, green, blue, alpha);
		if(ridges > 0){
		    if(drawHiddenLines){
			int ridgesHidden= model.getSmoothRidgesHidden(true, true, drawFaded, threshold, hiddenRed, hiddenGreen, hiddenBlue, hiddenAlpha);
			gl.glDisable(GL.GL_DEPTH_TEST);
			gl.glLineWidth(hiddenWidth);
			gl.glCallList(ridgesHidden);
			gl.glEnable(GL.GL_DEPTH_TEST);
		    }
		    gl.glLineWidth(width);
		    gl.glDepthMask(false);
		    gl.glCallList(ridges);
		    gl.glDepthMask(true);
		}
	    }
	    gl.glPopMatrix();
	}
	gl.glDisable(GL.GL_LINE_SMOOTH);
    }
    
}